﻿using System;
using TimeTracker.Entities;

namespace TimeTracker.Models
{
    partial class EntryModel
    {
        public Entry ToModel()
        {
            return new Entry
            {
                Id = Id,
                ClientId = ClientId,
                ProjectId = ProjectId,
                UserId = UserId,
                LastStartTime = DateTime.Now,
                ElapsedTime = ElapsedTime,
                Description = Description,
                IsActive = IsActive
            };
        }

        public static EntryModel FromModel(Entry entry, Client client, Project project)
        {
            return new EntryModel
            {
                Id = entry.Id,
                UserId = entry.UserId,
                ClientId = entry.ClientId,
                ProjectId = entry.ProjectId,
                Description = entry.Description,
                LastStartTime = entry.LastStartTime,
                ElapsedTime = entry.ElapsedTime,
                Client = client.Name,
                Project = project.Name,
                IsActive = entry.IsActive
            };
        }

        public void UpdateModel(Entry entry)
        {
            entry.Id = Id;
            entry.ClientId = ClientId;
            entry.ProjectId = ProjectId;
            entry.UserId = UserId;
            entry.LastStartTime = LastStartTime;
            entry.ElapsedTime = ElapsedTime;
            entry.Description = Description;
            entry.IsActive = IsActive;
        }

        public static EntryModel Create(User user, Client client, Project project)
        {
            return new EntryModel
            {
                UserId = user.Id,
                ClientId = client.Id,
                Client = client.Name,
                ProjectId = project.Id,
                Project = project.Name,
                IsActive = true
            };
        }
    }
}
